package main

import (
	"gitee.com/DonHz/simple-blog-system/handler"
	"gitee.com/DonHz/simple-blog-system/handler/middleware"
	"gitee.com/DonHz/simple-blog-system/model"
	"gitee.com/DonHz/simple-blog-system/util/jwtx"
	"gitee.com/DonHz/simple-blog-system/util/logger"
	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"io"
)

func main() {
	logger.InitLog("log")
	gin.SetMode(gin.ReleaseMode)	//Gin线上发布模式
	gin.DefaultWriter=io.Discard	// 禁止GIN输出
	db:=model.InitDB()
	UserDAO:=model.NewUserDAO(db)
	ArticleDAO:=model.NewArticleDAO(db)
	userHandler :=handler.NewUserHandler(*UserDAO)
	articleHandler :=handler.NewArticleHandler(*ArticleDAO)
	r:=gin.Default()
	r.Use(middleware.Metric())
	r.GET("/metrics", func(ctx *gin.Context) {
		promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request)
	})
	r.Static("/js", "views/js")
	r.StaticFile("/favicon.ico", "views/img/favicon.ico")
	r.LoadHTMLFiles("views/login.html", "views/blog.html", "views/blog_list.html")	//使用这些html文件
	r.GET("/login", func(ctx *gin.Context){
		ctx.HTML(200,"login.html", nil)
	})
	r.POST("/login/submit", userHandler.Login)
	r.POST("/token", jwtx.GetAuthToken)
	r.GET("/blog/belong", articleHandler.BlogBelong)
	r.GET("/blog/list/:uid", articleHandler.BlogList)
	r.GET("/blog/:aid", articleHandler.BlogDetail)
	r.POST("/blog/update", middleware.Auth(),articleHandler.BlogUpdate)
	r.Run(":1234")
}
